<?php
/**
 * @version 2014-07-01
 * @author Leo
 * 
 */
class Model_DbTable_Products_Price_Flexprice extends Zend_Db_Table_Abstract {

	protected $_name = 'product_flex_price';
	
	/**
	 * @version 2014-07-01
 	 * @author Leo
	 * @param int $idProduct
	 * get flex price by id
	 */

	public function getFprice( $idProduct, $idCategory){
		
		$row = $this->fetchRow("`id_product` = ". $idProduct.' AND '. "`id_category` = ". $idCategory);
		if(!$row) return false;
		return $row->toArray();		
		
		}
	/**
	 * @version 2014-07-01
 	 * @author Leo 
	 * @param int $idProduct
	 * @param int $idCategory
	 * @param date $reviewDate
	 * get price by id , category id and date
	 */	
	public function getPricebyidDate( $idProduct, $idCategory,$reviewDate = null){
		$row = $this->fetchRow("`id_product` = ". $idProduct.' AND '. "`id_category` = ". $idCategory.' AND '."`review_date` <= "."'".$reviewDate."'");
		if(!$row) return false;
		return $row->toArray();
	}
	
	/**
	 * @version 2014-07-01
 	 * @author Leo  
	 * @param int $idProduct
	 * List all detail by product id
	 */
	public function getPricebyid( $idProduct){
		
			$row = $this->fetchAll("`id_product` = ". $idProduct);
			if(!$row) return false;
			return $row->toArray();
		
	}
	/**
	 * @author Norman
	 * @version 2014-07-22
	 * @param unknown_type $idProduct
	 * @param unknown_type $idCategory
	 */
	public function getPriceByIDCategory($idProduct,$idCategory){
		
		$row = $this->fetchRow( '`id_product` = '. $idProduct.' AND `id_category` = '. $idCategory);
		if(!$row) return false;
		return $row->toArray();		
	}

	/**
	 * @version 2014-07-01
	 * @author Leo
	 * @param int $idCategory
	 * @param float $loveBargainPrice
	 * @param date $reviewDate
	 * insert flex price into product_flex_price
	 */
	public function addFprice( $idProduct , $idCategory , $flexPrice , $reviewDate){
		
		$data = array(
			
         "id_product" =>  $idProduct ,
         "id_category" =>  $idCategory ,
         "flex_price" =>  $flexPrice ,
         "review_date" =>  $reviewDate 
	
			);
		$this->insert($data);
		
		}
	/**
	 * @version 2014-07-01
	 * @author Leo
	 * @param int $idCategory
	 * @param int $typePrice
	 * @param float $loveBargainPrice
	 * @param date $reviewDate
	 * update flex price into product_flex_price
	 */
	public function updateFprice(  $idFlexPrice ,  $idProduct , $idCategory , $flexPrice , $reviewDate){
		$data = array(
			
         "id_product" =>  $idProduct ,
         "id_category" =>  $idCategory ,
         "flex_price" =>  $flexPrice ,
         "review_date" =>  $reviewDate 
	
			);
			
		$this->update($data,"`id_flex_price` = ". $idFlexPrice);
		}
	
	public function updateFlexprice($idProduct,$idCategory,$flexPrice,$reviewDate){
		$data = array(
				"id_product" => $idProduct,
				"flex_price" =>  $flexPrice ,
				"review_date" =>  $reviewDate);
		$this->update($data, "`id_product` = ". $idProduct.' AND '. "`id_category` = ". $idCategory);
					
	}
	
	/**
	 * @version 2014-07-01
	 * @author Leo
	 * @param int $idProduct
	 * delect by id
	 */
	public function deleteFprice( $idFlexPrice){
		
		$this->delete("`id_flex_price` = ". $idFlexPrice);
		
		}
	/**
	 * @version 2014-07-01
	 * @author Leo
	 * List all
	 */
	public function listAll(){
		
		$rows =$this->fetchAll("1","id_flex_price DESC ");
		if(!$rows) return false;
		return $rows->toArray();			
		
		}
	/**
	 * @version 2014-07-01
	 * @author Leo
	 * @param date $reviewDate
	 * list by date
	 
	 */
	public function listbyDate($reviewDate){
	
		$rows =$this->fetchAll("`review_date` <= "."'".$reviewDate."'");
		if(!$rows) return false;
		return $rows->toArray();
	
	}
	
	public function conditionalInsert( $idProduct , $idCategory , $flexPrice , $reviewDate){
		$idLine = self::getPriceByIDCategory($idProduct, $idCategory);
		
		if(!$idLine){
			
			self::addFprice($idProduct, $idCategory, $flexPrice, $reviewDate);
		}
		else{
			self::updateFprice($idLine['id_flex_price'], $idProduct, $idCategory, $flexPrice, $reviewDate);
		}
		
	
	}
}

?>